<?php
/**
 *	  商户分组服务层
 *	  by Ouyoung
 *
 */
class supplier_group_service extends service {
		
	protected $count;
    protected $pages;
	
	public function __construct() {
		$this->model = $this->load->table('supplier/supplier_group');
	}
    
	/**
     * 获分组列表
     * @param type $sqlmap
     * @return type
     */
    public function getlist($sqlmap = array()) {
        $groups = array();
        $groups['group'] = $this->model->where($sqlmap)->select();
        $groups['count'] = $this->model->where($sqlmap)->count();
		return $groups;
    }

	/**
     * 查询单个信息
     * @param int $id 主键ID
     * @param string $field 被查询字段
     * @return mixed
     */
    public function fetch_by_id($id, $field = NULL) {
        $r = $this->model->find($id);
        if(!$r) return FALSE;
        return ($field !== NULL) ? $r[$field] : $r;
    }

	/**
	 * [更新商家分组]
	 * @param array $data 数据
	 * @param bool $valid 是否M验证
	 * @return bool
	 */
	public function save($data, $valid = FALSE) {
		if($valid == TRUE){
			$data = $this->model->create($data);
			$result = $this->model->add($data);
		}else{
			$result = $this->model->update($data);
		}
		return $result;
	}
	/**
	 * [启用禁用分组]
	 * @param string $id 分组ID
	 * @return TRUE OR ERROR
	 */
	public function change_status($id) {
		$result = $this->model->where(array('id' => $id))->save(array('status' => array('exp', '1-status')));
		if ($result == 1) {
			$result = TRUE;
		} else {
			$result = $this->model->getError();
		}
		return $result;
	}
    
	/**
	 * [ajax_del 删除分组，删除只改变状态，在回收站里删除直接删除]
	 * @param  [type] $params 
	 * @return bool         TRUE OR FALSE
	 */
	public function ajax_del($params){
		$id = $params['id'];
		$label = $params['label'];
		$data = $sqlmap = array();
		if($id){
			if($label == 4){
				$result =$this->delete_groups($id);
				return $result;
			}else{
				$sqlmap['id'] = array('IN',$id);
				$data['status'] = -1;
				$result = $this->model->where($sqlmap)->save($data);
				if(!$result){
					$this->error = lang('_operation_fail_');
					return FALSE;
				}
				return TRUE;
			}
		}else{
			$this->error = lang('_param_error_');
			return FALSE;
		}
	}
    
	/**
	 * [delete_groups 删除分组,只有在回收站里进行此操作]
	 * @param  [array] $id [分组id]
	 * @return [type]     [description]
	 */
	private function delete_groups($id){
		$id = (array)$id;
		if(empty($id)) {
			$this->error = lang('_param_error_');
			return FALSE;
		}
		$_groups_del_result = $this->model->where(array('id' => array("IN", $id)))->delete();
		return true;
	}
    
	/**
	 * [更新商户分组名称]
	 * @param array $params 数据
	 * @return bool
	 */
	public function ajax_save_name($params){
		if((int)$params['id'] < 1){
			$this->error = lang('_param_error_');
			return FALSE;
		}
		$data = array();
		$data['title'] = $params['title'];
		$result = $this->model->where(array('id'=>$params['id']))->save($data);
		if(!$result){
    		$this->error = lang('_operation_fail_');
    	}
    	return $result;
	}
    
	/**
	 * [ajax_recover 批量恢复商家分组]
	 * @param  [array] $id [要恢复的商家分组id]
	 * @return [type]     [description]
	 */
	public function ajax_recover($id){
		$id = (array)$id;
		if(empty($id)) {
			$this->error = lang('_param_error_');
			return FALSE;
		}
		$data = array();
			$sqlmap['id'] = array('IN',$id);
			$data['status'] = 1;
			$result = $this->model->where($sqlmap)->save($data);
		if(!$result){
			$this->error = lang('_operation_fail_');
		}
		return $result;
	}
 }